package net.wanglu.www.zzz.rule.artcl;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.service.se.np._NPTuple;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;

public class Plural extends _ArticleRule {

	public final int PluralFlag = 1;
	public final int SingularFlag = 2;
	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();
		if ( NP.depth() != 2) return Rule.Inapplicable;//如果不是叶子节点的父母则不符合要求
		TaggedWord headword = npt.getHeadword();
		if ( "NN".equals(headword.tag()) || "NNS".equals(headword.tag()) || "NNP".equals(headword.tag()) || "NNPS".equals(headword.tag())) {
			return Rule.Applicable;//如果中心词是名词则符合要求，否则不符合
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( this.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		
		_NPTuple npt = (_NPTuple) objects[1];
		Tree NP = npt.getNP();
		
		TregexPattern pattern = null;
		TregexMatcher matcher = null;

		/*
		 * 名词短语中含有的冠词，也可能没有
		 */
		Tree dt = null;
		sb.clear();
		sb.append("/\\bDT\\b/>/\\bNP\\b/");
		pattern = TregexPattern.compile(sb.toString());
		matcher = pattern.matcher(NP);
		if ( matcher.find()) {
			dt = matcher.getMatch();
		}
		/*
		 * 名词短语中的headword
		 */
		TaggedWord headword = npt.getHeadword();

		if ( "NNS".equals(headword.tag()) || "NNPS".equals(headword.tag())) {//中心词是复数
			if ( dt == null) {//名词短语中没有冠词
				return null;
			} else {//名词短语中含有冠词
				String dtWord = dt.taggedYield().get(0).word();
				if ( "an".equalsIgnoreCase(dtWord) || "a".equalsIgnoreCase(dtWord)) {
					BadEgg egg = new BadEgg();
					egg.whereIsWrong = NP;
					egg.setProperty(BadEgg.Error);
					egg.setFlag(this.PluralFlag);
					egg.setViolatedRule(this);
					return egg;						
				} else if( "the".equalsIgnoreCase(dtWord)) {
					return null;
				} else {//如果是其他DT，比如指示代词this/that等则不符合要求
					return Rule.Inapplicable;
				}
			}			
		} else if ( "NN".equals(headword.tag()) || "NNP".equals(headword.tag())) {//中心词是单数
			if ( dt == null) {//名词短语中没有冠词
				BadEgg egg = new BadEgg();
				egg.whereIsWrong = NP;
				egg.setProperty(BadEgg.Warning);
				egg.setFlag(this.SingularFlag);
				egg.setViolatedRule(this);
				return egg;					
			} else {//名词短语中含有冠词
				String dtWord = dt.taggedYield().get(0).word();
				if ( "an".equalsIgnoreCase(dtWord) || "a".equalsIgnoreCase(dtWord) || "the".equalsIgnoreCase(dtWord)) {
					return null;
				} else {//如果是其他DT，比如指示代词this/that等则不符合要求
					return Rule.Inapplicable;
				}
			}
		} else {
			return Rule.Inapplicable;
		}
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		BadEgg egg = (BadEgg) objects[0];
		sb.clear();
		if ( egg.getFlag() == this.SingularFlag) {
			sb.append("<p>");
			sb.append("可数名词单数如果是泛指在前面要加冠词a或an，如果是特指则可以在前面加冠词the。");
			sb.append("<br>");
			sb.append("如果是不可数名词，如果是泛指在前面不需要加冠词，如果是特指则可以在前面加冠词the。");
			sb.append("</p>");			
		} else if ( egg.getFlag() == this.PluralFlag) {
			sb.append("<p>");
			sb.append("可数名词复数如果是泛指在前面不需要加冠词a或an，如果是特指则可以在前面加冠词the。");
			sb.append("</p>");
		}
		return sb.toString();
	}
}
